package com.dianrui.operation.activity;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.dianrui.operation.R;
import com.dianrui.operation.util.ToastUtil;
import com.xiaoantech.sdk.XiaoanBleApiClient;
import com.xiaoantech.sdk.ble.model.Response;
import com.xiaoantech.sdk.listeners.BleCallback;
import com.xiaoantech.sdk.listeners.BleStateChangeListener;


import butterknife.BindView;
import butterknife.OnClick;
import permissions.dispatcher.NeedsPermission;

/**
 * 小安纯蓝牙下发指令
 */
public class XiaoAnCommandActivity extends BaseActivity implements BleStateChangeListener {
    private boolean isConnect = false;
    private XiaoanBleApiClient apiClient;
    private String devices;

    @BindView(R.id.title)
    TextView title;

    @Override
    public int getContentViewId() {
        return R.layout.activity_xiaoancommand;
    }

    @Override
    public void initView() {
        XiaoanBleApiClient.Builder builder = new XiaoanBleApiClient.Builder(this);
        builder.setBleStateChangeListener(this);
        apiClient = builder.build();
        title.setText("下发指令");
        if (getIntent() != null && getIntent().getExtras() != null) {
            devices = getIntent().getStringExtra("number");
            connectBlueTeeth(devices);
        }
    }


    @Override
    public void initData() {}


    /**连接蓝牙**/
    @NeedsPermission({Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.BLUETOOTH,Manifest.permission.BLUETOOTH_ADMIN})
    public void connectBlueTeeth(String imei) {
        showLoadingDialog("正在连接蓝牙...");
        if (null != apiClient) {
            apiClient.connectToIMEI(imei);
        }

    }

    @Override
    public void onConnect(BluetoothDevice bluetoothDevice) {//设备在身边走连接成功或者连接失败，不在就乱了
        isConnect = true;
        dismissLoadingDialog();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.showToast("蓝牙连接成功");

            }
        });
    }

    @Override
    public void onDisConnect(BluetoothDevice bluetoothDevice) {
        isConnect = false;
        dismissLoadingDialog();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                ToastUtil.showToast("蓝牙连接失败,请重新连接");
            }
        });
    }

    @Override
    public void onDeviceReady(BluetoothDevice bluetoothDevice) {
        dismissLoadingDialog();
        Log.e("设备准备好", bluetoothDevice.getName());

    }

    @Override
    public void onReadRemoteRssi(int rssi) {

    }

    @Override
    public void onError(BluetoothDevice device, String message, int errorCode) {
        dismissLoadingDialog();
        Log.e("设备错误---->", message);
    }

    @Override
    public void onBleAdapterStateChanged(int state) {
        dismissLoadingDialog();
        Log.e("设备状态改变---->", state + "");
    }

    public void shutdown() {//关闭
        apiClient.shutdown(new BleCallback() {
            @Override
            public void onResponse(Response response) {
                dismissLoadingDialog();
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }
            }
        });
    }

    //寻车
    public void findcar() {
        apiClient.findCar(new BleCallback() {
            @Override
            public void onResponse(Response response) {
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }
            }
        });
    }

    //电门开
    public void startacc() {
        apiClient.setAcc(true, new BleCallback() {
            @Override
            public void onResponse(final Response response) {
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }

            }
        });
    }

    //电门关
    public void startunacc() {
        apiClient.setAcc(false, new BleCallback() {
            @Override
            public void onResponse(final Response response) {
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }
            }
        });
    }

    //设防
    public void setdefend() {
        apiClient.setDefend(true, new BleCallback() {
            @Override
            public void onResponse(final Response response) {
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }

            }
        });
    }

    //撤防
    public void setundefend() {
        apiClient.setDefend(false, new BleCallback() {
            @Override
            public void onResponse(final Response response) {
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }

            }
        });
    }

    public void fenceClick() {
        apiClient.setDefend(true, new BleCallback() {
            @Override
            public void onResponse(final Response response) {
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }
            }
        });
    }

    //鞍座
    public void seatClick() {
        apiClient.setSaddle(true, new BleCallback() {
            @Override
            public void onResponse(final Response response) {
                Log.e("seat: ", response.toString());
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }
            }
        });
    }

    //锁后轮
    public void wheelClick() {
        apiClient.setBackWheel(true, new BleCallback() {
            @Override
            public void onResponse(final Response response) {
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }
            }
        });
    }
   //解锁后轮
    public void unwheelClick() {
        apiClient.setBackWheel(false, new BleCallback() {
            @Override
            public void onResponse(final Response response) {
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }
            }
        });
    }


    //电池仓开
    public void setsaddle() {
        apiClient.setSaddle(true, new BleCallback() {
            @Override
            public void onResponse(Response response) {
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }
            }
        });
    }

    //电池仓关
    public void setsaddleoff() {
        apiClient.setSaddle(false, new BleCallback() {
            @Override
            public void onResponse(Response response) {
                if (response.code==0) {
                    ToastUtil.showToast("蓝牙命令成功");
                }else {
                    ToastUtil.showToast("蓝牙命令失败");
                }
            }
        });
    }


    @OnClick({R.id.back, R.id.find_car, R.id.acc_on, R.id.acc_off, R.id.defend, R.id.undefend, R.id.wheel_lock, R.id.wheel_unlock, R.id.saddle, R.id.saddle_off})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                if (apiClient != null) {
                    apiClient.disConnect();//关闭界面就马上断开，释放资源
                }
                finish();
                break;
            case R.id.find_car:
                findcar();
                break;
            case R.id.acc_on:
                startacc();
                break;
            case R.id.acc_off:
                startunacc();
                break;
            case R.id.defend:
                setdefend();
                break;
            case R.id.undefend:
                setundefend();
                break;
            case R.id.wheel_lock:
                wheelClick();
                break;
            case R.id.wheel_unlock:
                unwheelClick();
                break;
            case R.id.saddle:
                setsaddle();
                break;

            case R.id.saddle_off:
                setsaddleoff();
                break;

            default:
                break;
        }
    }




    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            apiClient.disConnect();
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (apiClient != null) {
            apiClient.disConnect();
            apiClient.onDestroy();
        }


    }
}
